package com.nuanshui.heatedloan.fund.integration.pay.execute.bizexcute;

import com.alibaba.dubbo.config.annotation.Reference;
import com.nuanshui.framework.utils.LogUtil;
import com.nuanshui.heatedloan.baseservice.facade.OrderNumGenerate;
import com.nuanshui.heatedloan.constant.GlobalParam;
import com.nuanshui.heatedloan.constant.RabbitMqQueue;
import com.nuanshui.heatedloan.fund.enums.PaymentBizType;
import com.nuanshui.heatedloan.fund.facade.OrderNumWithPayNumService;
import com.nuanshui.heatedloan.fund.integration.form.CancelPayForm;
import com.nuanshui.heatedloan.fund.integration.form.PayForm;
import com.nuanshui.heatedloan.fund.integration.form.RequestPayForm;
import com.nuanshui.heatedloan.fund.integration.pay.annotation.PayHandlerComponent;
import com.nuanshui.heatedloan.fund.integration.pay.execute.sysexecute.AbstractPayExecute;
import com.nuanshui.heatedloan.fund.model.OrderNumWithPayNum;
import com.nuanshui.heatedloan.fund.model.PayResult;
import com.nuanshui.heatedloan.middleware.rabbitmq.RabbitmqMessageSender;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * 预约订单的支付
 * @Author: Gonzo
 * @Date 2017/4/13下午4:18
 * @since 1.4.2
 */
@PayHandlerComponent(value="appointmentOrderPayExecute",bizType= PaymentBizType.APPOINTMENT_ORDER_PAY)
public class AppointmentOrderPayExecute extends AbstractPayExecute {

    private org.slf4j.Logger logger = LoggerFactory.getLogger(AppointmentOrderPayExecute.class);

    @Reference(url= GlobalParam.FUND_DUBBO_SERVER,timeout=GlobalParam.DUBBO_SERVER_TIME_OUT,version=GlobalParam.FUND_DUBBO_SERVER_VERSION)
    private OrderNumWithPayNumService orderNumWithPayNumService;

    @Reference(url = GlobalParam.BASESERVICE_DUBBO_SERVER, timeout = GlobalParam.DUBBO_SERVER_TIME_OUT,version = GlobalParam.BASESERVICE_DUBBO_SERVER_VERSION)
    private OrderNumGenerate orderNumGenerate;

    @Autowired
    private RabbitmqMessageSender rabbitmqMessageSender;

    @Override
    public String doLoadPayNum(RequestPayForm requestPayForm) {
        String payNum = orderNumGenerate.getDealNum(PaymentBizType.APPOINTMENT_ORDER_PAY.getCode());
        OrderNumWithPayNum orderNumWithPayNum = new OrderNumWithPayNum();
        orderNumWithPayNum.setMoney(requestPayForm.getMoney().intValue());
        orderNumWithPayNum.setOrderNum(requestPayForm.getOrderNum());
        orderNumWithPayNum.setPayNum(payNum);
        orderNumWithPayNumService.save(orderNumWithPayNum);
        LogUtil.info(logger, String.format("-------订单号:%s 生成的支付号是: %s", requestPayForm.getOrderNum(), payNum));
        return payNum;
    }

    @Override
    public boolean doCancelPay(CancelPayForm payForm) {
        return false;
    }

    @Override
    public PayResult doConfirmPay(PayForm payForm, PayResult payResult) {
        String orderNum = orderNumWithPayNumService.getOrderNumByPayNum(payForm.getOrderNum());
        //获取到支付的订单号, 发送消息, 将订单置为支付成功
        rabbitmqMessageSender.send(RabbitMqQueue.RABBIT_MQ_MUSEUM_ORDER_PAY_SUCCESS, orderNum, true);

        LogUtil.info(logger, String.format("-------发送订单号是: %s---的确定订单消息, 成功-------", orderNum));
        return payResult;
    }

    /**
     * 处理生成账单需要的一些信息
     * @param @param payResult
     * @author guohao
     * @date 2017年1月11日 上午10:21:46
     * @since $
     *
     */
    @Override
    public void handlerAccountBillInfo(PayResult payResult,PayForm payForm) {

    }
}
